package com.situ.mall.controller.back;

import java.util.Arrays;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Product;
import com.situ.mall.entity.Result;
import com.situ.mall.service.IUserService;
import com.situ.mall.entity.User;

@Controller 
@RequestMapping("/user")
public class UserController {
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/login.action")
	@ResponseBody
	public Result login (String username, String password,HttpSession session) {
		System.out.println("UserController.login()");
		System.out.println("name: " + username + ", password: " + password);
		User user = userService.login(username, password);
		
		// Result result = new Result(1, "登录成功");
		if (user != null) {
			session.setAttribute("user", user);
			return Result.success("登录成功");
		} else {
			return Result.error("登录失败");
		}
	}

	//newLogin登录后台
	@RequestMapping("/loginIndex.action")
	@ResponseBody
	public Result loginIndex (String username, String password,HttpSession session) {
		System.out.println("UserController.loginIndex()");
		System.out.println("name: " + username + ", password: " + password);

		User user = userService.login(username, password);
		System.out.println("user = " + user);
		// Result result = new Result(1, "登录成功");
		if (user != null) {
			int role = user.getRole();
			if (role == 0) {
				session.setAttribute("user", user);
				return Result.success("登录成功");
			}else {
				session.setAttribute("user", user);
				return Result.error("您的权限不足，无法进入后台");
			}
		} else {
			return Result.error("登录失败");
		}
	}

	@RequestMapping("/getLoginListPage.action")
	public String getLoginListPage() {
		return "login";
	}

	@RequestMapping("/roleLogin.action")
	public String roleLogin() {
		return "roleLogin";
	}
	
	@RequestMapping("/getUserListPage.action")
	public String getUserListPage() {
		System.out.println("UserController.getUserListPage()");
		return "user_list";
	}
	
	@RequestMapping("/ExitUser.action")
	public Result ExitUser(HttpSession session)
    {
		User user = (User)session.getAttribute("user");
        session.invalidate();
		return Result.success("已退出当前用户");
    }
	
	
	@RequestMapping("/selectByPage.action")
	@ResponseBody
	public Result selectByPage(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "5")Integer limit,User user) {
		System.out.println("UserController.selectByPage()");
		System.out.println("user = " + user);
		Result result = userService.selectByPage(page, limit,user);
		return result;
	}
	
	@RequestMapping("/getInsertUserPage.action")
	public String insertUser() {
		System.out.println("正在进入添加用户界面");
		return "user_insert";
	}

	@RequestMapping("/getUpdateUser.action")
	public String getUpdateUser(Integer UserId,Model model) {
		System.out.println("正在进入修改用户界面");
		System.out.println("UserId = " + UserId);
		User user = userService.getUpdateUserInfo(UserId);
		
		model.addAttribute("user", user);
		return "user_update";
	}
	
	@RequestMapping("/insertUser.action")
	@ResponseBody
	public Result insertUser(User user) {
		System.out.println(user);
		
		boolean isSuccess = userService.insertUser(user);
		System.out.println(isSuccess);
		if (isSuccess) {
			return Result.success("添加用户"+user.getUsername()+"成功");
		}else {
			return Result.error("添加失败");
		}
	}

	@RequestMapping("/selectUserByName")
	@ResponseBody
	public Result selectUserByName(User user) {
		System.out.println(user);
		User selectUser = userService.selectByUserName(user.getUsername(),user.getId());
		System.out.println("selectUser = " + selectUser);
		
		if(selectUser == null) {
			updateUser(user);
			return Result.success("修改成功");
		}else {
			return Result.error("用户名“"+selectUser.getUsername()+"”重名，不可修改");
			
		}
		
	}
	
	@RequestMapping("/updateUser.action")
	@ResponseBody
	public Result updateUser(User user) {
		System.out.println(user);
		boolean isSuccess = userService.updateUser(user);
		System.out.println(isSuccess);
		if (isSuccess) {
			return Result.success("修改用户"+user.getUsername()+"成功");
		}else {
			return Result.error("添加失败");
		}
	}
	
	@RequestMapping("/deleteById")
	@ResponseBody
	public Result deleteById(Integer id) {
		System.out.println("UserController.deleteById()");
		System.out.println("id = " + id);
		
		boolean isSuccess = userService.deleteById(id);
		System.out.println(isSuccess);
		if (isSuccess) {
			return Result.success("删除用户成功");
		}else {
			return Result.error("添加失败");
		}
	}
	
	@RequestMapping("/deleteAll.action")
	@ResponseBody
	public Result deleteAll(/* String ids */ Integer[] ids) {
		// System.out.println("ids: " + ids);
		// String[] array = ids.split(",");
		System.out.println(Arrays.toString(ids));// [26, 27]
		boolean isSuccess = userService.deleteAll(ids);
		if (isSuccess) {
			return Result.success("删除成功");
		} else {
			return Result.error("删除失败");
		}
	}
	
}
